package com.devframe.component.ftlprocess.freemarker.handle;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;

import org.apache.log4j.Logger;

import com.devframe.component.ftlprocess.freemarker.context.MyFTLProcessHandleContext;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;


/**
 * 说明：report标记的解析
 * 
 * @创建：作者:詹保山(zbs),softzbs@126.com 创建时间：
 * @修改历史： [序号](zbs Sep 18, 2009)<修改说明>
 */
public class ReportHandler {
	private static Logger logger = Logger.getLogger(ReportHandler.class);
	private MyFTLProcessHandleContext handleContext;
	private Configuration configuration;
	
	public ReportHandler(MyFTLProcessHandleContext handleContext ){
		this.handleContext=handleContext;
		this.configuration=this.handleContext.getReportConfiguration();
	}
	public  String process(String templateName, Map<String, Object> root){
		Template temp;
		Writer writer =null;
		try {
			temp = this.configuration.getTemplate(templateName);
			//加入公共的指令
			root.putAll(this.handleContext.getDirectives());
			writer = new StringWriter();
			temp.process(root, writer);
			writer.flush();
			writer.close();
			return writer.toString();
			
		} catch (IOException e) {
			e.printStackTrace();
			logger.error("解析模板保存，未找到对应模板文件"+e);
		} catch (TemplateException e) {
			logger.error("模板解析出错"+e);
		}finally{
			temp=null;
			writer=null;
		}
		return null;
	}
	
	
	

}
